package ca.liu.scaffoldingmvctest.dao;

import ca.liu.scaffoldingmvctest.domain.StUser;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage StUser entities.
 * 
 */
@Repository("StUserDAO")
@Transactional
public class StUserDAOImpl extends AbstractJpaDao<StUser> implements StUserDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { StUser.class }));

	/**
	 * EntityManager injected by Spring for persistence unit org_postgresql_Driver
	 *
	 */
	@PersistenceContext(unitName = "org_postgresql_Driver")
	private EntityManager entityManager;

	/**
	 * Instantiates a new StUserDAOImpl
	 *
	 */
	public StUserDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findStUserByUsername
	 *
	 */
	@Transactional
	public Set<StUser> findStUserByUsername(String username) throws DataAccessException {

		return findStUserByUsername(username, -1, -1);
	}

	/**
	 * JPQL Query - findStUserByUsername
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<StUser> findStUserByUsername(String username, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStUserByUsername", startResult, maxRows, username);
		return new LinkedHashSet<StUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findStUserByUsernameContaining
	 *
	 */
	@Transactional
	public Set<StUser> findStUserByUsernameContaining(String username) throws DataAccessException {

		return findStUserByUsernameContaining(username, -1, -1);
	}

	/**
	 * JPQL Query - findStUserByUsernameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<StUser> findStUserByUsernameContaining(String username, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStUserByUsernameContaining", startResult, maxRows, username);
		return new LinkedHashSet<StUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findStUserById
	 *
	 */
	@Transactional
	public StUser findStUserById(Integer id) throws DataAccessException {

		return findStUserById(id, -1, -1);
	}

	/**
	 * JPQL Query - findStUserById
	 *
	 */

	@Transactional
	public StUser findStUserById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStUserById", startResult, maxRows, id);
			return (ca.liu.scaffoldingmvctest.domain.StUser) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllStUsers
	 *
	 */
	@Transactional
	public Set<StUser> findAllStUsers() throws DataAccessException {

		return findAllStUsers(-1, -1);
	}

	/**
	 * JPQL Query - findAllStUsers
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<StUser> findAllStUsers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllStUsers", startResult, maxRows);
		return new LinkedHashSet<StUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findStUserByPrimaryKey
	 *
	 */
	@Transactional
	public StUser findStUserByPrimaryKey(Integer id) throws DataAccessException {

		return findStUserByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findStUserByPrimaryKey
	 *
	 */

	@Transactional
	public StUser findStUserByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStUserByPrimaryKey", startResult, maxRows, id);
			return (ca.liu.scaffoldingmvctest.domain.StUser) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findStUserByPassword
	 *
	 */
	@Transactional
	public Set<StUser> findStUserByPassword(String password) throws DataAccessException {

		return findStUserByPassword(password, -1, -1);
	}

	/**
	 * JPQL Query - findStUserByPassword
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<StUser> findStUserByPassword(String password, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStUserByPassword", startResult, maxRows, password);
		return new LinkedHashSet<StUser>(query.getResultList());
	}

	/**
	 * JPQL Query - findStUserByPasswordContaining
	 *
	 */
	@Transactional
	public Set<StUser> findStUserByPasswordContaining(String password) throws DataAccessException {

		return findStUserByPasswordContaining(password, -1, -1);
	}

	/**
	 * JPQL Query - findStUserByPasswordContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<StUser> findStUserByPasswordContaining(String password, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStUserByPasswordContaining", startResult, maxRows, password);
		return new LinkedHashSet<StUser>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(StUser entity) {
		return true;
	}
}
